REM import conf variables 
call config.bat

REM change to test directory
cd %TEST_PATH% 

REM Create test database 
echo "Create test database "
if %TEST_STEP_RUN%==1 ( msg /w "%username%" 'Create test database ')
echo 'mysql --host=%MYSQL_HOST% --user=%MYSQL_USER% --password=%MYSQL_PASS% < %TEST_PATH%\amp3test_BIG_create.sql'
mysql --host=%MYSQL_HOST% --user=%MYSQL_USER% --password=%MYSQL_PASS% < %TEST_PATH%\amp3test_BIG_create.sql



REM Delete RESULT folder if exists
echo "Delete RESULT folder if exists"
if %TEST_STEP_RUN%==1 ( msg /w "%username%" 'Delete RESULT folder if exists: rmdir /S /Q RESULT')
rmdir /S /Q RESULT

REM Create RESULT folder
echo "Create RESULT folder"
if %TEST_STEP_RUN%==1 ( msg /w "%username%" 'Create RESULT folder: mkdir RESULT' )
mkdir RESULT

REM Clean AMP Server Logs
echo "Clean AMP Server Logs"
if %TEST_STEP_RUN%==1 ( msg /w "%username%" 'Clean AMP Server Logs: del /F /Q %AMP_SERVER_PATH%\platform-dist\server\logs\*' )
echo 'rm %AMP_SERVER_PATH%\platform-dist\server\logs\*' >> test.log
del /F /Q %AMP_SERVER_PATH%\platform-dist\server\logs\*

REM Starting AMP Server
echo "Starting AMP Server"
if %TEST_STEP_RUN%==1 ( msg /w "%username%" 'Starting AMP Server: start /D%AMP_SERVER_PATH%\platform-dist\server\bin startAMP.bat' )
echo '%AMP_SERVER_PATH%\platform-dist\server\bin\startAMP.bat' >> test.log
start /D%AMP_SERVER_PATH%\platform-dist\server\bin startAMP.bat

REM Starting AVD 
echo 'Starting AVD %AVD_NAME%'
if %TEST_STEP_RUN%==1 ( msg /w "%username%" 'Starting AVD %AVD_NAME%: start %ANDROID_EMULATOR% @%AVD_NAME%' )
echo '%ANDROID_EMULATOR% @%AVD_NAME%' >> test.log
start %ANDROID_EMULATOR% @%AVD_NAME%

REM Wait for everything to start (AMP Server and AND emulator
echo 'Wait'
if %TEST_STEP_RUN%==1 ( msg /w "%username%" 'Wait: TIMEOUT /T 45' )
echo 'TIMEOUT /T 45' >> test.log
TIMEOUT /T 45

REM Uninstall tested app (event if its not installed) from AVD
if %TEST_STEP_RUN%==1 ( msg /w "%username%" 'Uninstall tested app from AVD' )
echo '%ANDROID_ADB% uninstall %TEST_APP_PACKAGE_NAME%' >> test.log
%ANDROID_ADB% uninstall %TEST_APP_PACKAGE_NAME%

REM Install tested app to AVD
if %TEST_STEP_RUN%==1 ( msg /w "%username%" 'Install tested app to AVD: %ANDROID_ADB% install %TEST_APP%' )
%ANDROID_ADB% install %TEST_APP%

REM Unlock AVD emulator after start
echo 'Run script andavd_unlock.sikuli'
if %TEST_STEP_RUN%==1 ( msg /w "%username%" 'Run script andavd_unlock.sikuli' )
echo 'java -Xms64M -Xmx512M -Dfile.encoding=UTF-8 -jar "%SIKULI_HOME%\sikuli-script.jar" "%TEST_SCRIPTS_PATH%\andavd_unlock.sikuli" %AVD_NAME% 2>&1' >> test.log
call java -Xms64M -Xmx512M -Dfile.encoding=UTF-8 -jar "%SIKULI_HOME%\sikuli-script.jar" "%TEST_SCRIPTS_PATH%\andavd_unlock.sikuli" %AVD_NAME% 2>&1

REM Run andavd_app_start.sikuli
echo 'Run andavd_app_start.sikuli'
if %TEST_STEP_RUN%==1 ( msg /w "%username%" 'Run andavd_app_start.sikuli' )
echo 'java -Xms64M -Xmx512M -Dfile.encoding=UTF-8 -jar "%SIKULI_HOME%\sikuli-script.jar" "%TEST_PATH%\andavd_app_start.sikuli" %AVD_NAME% "%TEST_PATH%\RESULT\test_result.csv" 2>&1' >> test.log
call java -Xms64M -Xmx512M -Dfile.encoding=UTF-8 -jar "%SIKULI_HOME%\sikuli-script.jar" "%TEST_PATH%\andavd_app_start.sikuli" %AVD_NAME% "%TEST_PATH%\RESULT\test_result.csv" 2>&1
echo 'andavd_app_start.sikuli finished'

REM Run andavd_app_login.sikuli
echo 'Run andavd_app_login_NC.sikuli'
if %TEST_STEP_RUN%==1 ( msg /w "%username%" 'Run andavd_app_login_NC.sikuli' )
echo 'java -Xms64M -Xmx512M -Dfile.encoding=UTF-8 -jar "%SIKULI_HOME%\sikuli-script.jar" "%TEST_PATH%\andavd_app_login_NC.sikuli" %AVD_NAME% "%TEST_PATH%\RESULT\test_result.csv" 2>&1' >> test.log
call java -Xms64M -Xmx512M -Dfile.encoding=UTF-8 -jar "%SIKULI_HOME%\sikuli-script.jar" "%TEST_PATH%\andavd_app_login_NC.sikuli" %AVD_NAME% "%TEST_PATH%\RESULT\test_result.csv" 2>&1
echo 'andavd_app_login_NC.sikuli finished'

REM TODO some inteligent way to wait for pool to finish 
msg /w /TIME:7200 "%username%" 'click ok after sync finish'

REM Archiwe SmartClient database
echo 'Archiwe SmartClient database'
if %TEST_STEP_RUN%==1 ( msg /w "%username%" 'Archiwe SmartClient database' )
echo 'mkdir RESULT\DEVICE_APP_FOLDER' >> test.log
mkdir RESULT\DEVICE_APP_FOLDER
echo '%ANDROID_ADB% pull "/data/data/%TEST_APP_PACKAGE_NAME%" "%TEST_PATH%\RESULT\DEVICE_APP_FOLDER' >> test.log
%ANDROID_ADB% pull "/data/data/%TEST_APP_PACKAGE_NAME%" "%TEST_PATH%\RESULT\DEVICE_APP_FOLDER

REM Archiwe AMP Server Logs
echo 'Archiwe AMP Server Logs'
if %TEST_STEP_RUN%==1 ( msg /w "%username%" 'Archiwe AMP Server Logs' )
echo 'mkdir RESULT\AMP_SERVER_LOGS' >> test.log
mkdir RESULT\AMP_SERVER_LOGS
echo 'cp %AMP_SERVER_PATH%\platform-dist\server\logs\* RESULT\AMP_SERVER_LOGS' >> test.log
cp %AMP_SERVER_PATH%\platform-dist\server\logs\* RESULT\AMP_SERVER_LOGS

REM Parse AMP Server Logs
echo 'Parse AMP Server Logs'
if %TEST_STEP_RUN%==1 ( msg /w "%username%" 'Parse AMP Server Logs' )
python parse_amp_server_logs.py %TEST_PATH%\RESULT\AMP_SERVER_LOGS\amp_server.log %TEST_PATH%\RESULT\test_perf_result.csv

echo 'TEST END'
msg /w "%username%" "TEST END"
pause
